<?php 
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

class Model
{
    protected $_db;
    protected $_config;
    protected $_setting = 'default';
    protected $_table = '';
    protected $_tablePre = '';
    protected $_dbName = '';
    
    public function __construct() {
        $this->_config = Db_Factory::getConfig();
        
        if (!isset($this->_config[$this->_setting])) {
            $this->_setting = 'default';
        }
        
        if('' ==  $this->_tablePre) {
            $this->_tablePre = $this->_config[$this->_setting]['tablepre'];
        }
        
        if('' ==  $this->_dbName) {
            $this->_dbName = $this->_config[$this->_setting]['database'];
        }

        $this->_table = $this->_tablePre . $this->_table;
        
        $this->_db = Db_Factory::getDatabase($this->_dbName, $this->_setting);
    }
        
    public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') {
        if (is_array($where)) {
            $where = $this->sqls($where);
        }
        
        return $this->_db->select($data, $this->_table, $where, $limit, $order, $group, $key);
    }

    public function listInfo($where = '', $page = 1, $perpage = 10, $order = '', $urlrule = '', $data = '*', $key = '') {
        if (is_array($where)) {
            $where = $this->sqls($where);
        }
        
        $sql = 'SELECT ' . $data . ' FROM ' . $this->_table . ($where == '' ? '' : ' WHERE ' . $where);
        return pages($this->_db->link, $sql, $page, $perpage, $order, $urlrule, $key);
    }

    public function getOne($where = '', $data = '*', $order = '', $group = '') {
        if (is_array($where)) {
            $where = $this->sqls($where);
        }
        
        return $this->_db->get_one($data, $this->_table, $where, $order, $group);
    }
    
    public function query($sql) {
        return $this->_db->query($sql);
    }
    
    public function fetchNext($type = MYSQL_ASSOC) {
        return $this->_db->fetch_next($type);
    }
    
    public function insert($data, $return_insert_id = false, $replace = false) {
        return $this->_db->insert($data, $this->_table, $return_insert_id, $replace);
    }
    
    public function insertId() {
        return $this->_db->insert_id();
    }
    
    public function update($data, $where = '') {
        if (is_array($where)) {
            $where = $this->sqls($where);
        }
        
        return $this->_db->update($data, $this->_table, $where);
    }
    
    public function delete($where) {
        if (is_array($where)) {
            $where = $this->sqls($where);
        }
        
        return $this->_db->delete($this->_table, $where);
    }
    
    public function count($where = '') {
        if (is_array($where)) {
            $where = $this->sqls($where);
        }
        
        $r = $this->getOne($where, "COUNT(*) AS num");
        return $r['num'];
    }
    
    public function sqls($where, $font = ' AND ') {
        if (is_array($where)) {
            $sql = '';
            foreach ($where as $key => $val) {
                $sql .= $sql ? " $font `$key` = '$val' " : " `$key` = '$val'";
            }
            return $sql;
        } else {
            return $where;
        }
    }
    
    public function affectedRows() {
        return $this->_db->affected_rows();
    }
}